﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace TTCaller
{
    public partial class SysConfigPanel : UserControl
    {

        bool m_bLogined = false;
        public SysConfigPanel()
        {
            InitializeComponent();
            LogManager.getInstance().Log("Initial SysConfig..");
            uint n = DeviceManager.readBackLightTimeOut();
            n = foundIndex(n, ref nBLTimeout );
            if (n == nBLTimeout.Length - 1)
                this.label_BLTimeout.Text = "背光時間：恒亮";
            else
                this.label_BLTimeout.Text = "背光時間：" + nBLTimeout[n].ToString() + "秒";

            n = DeviceManager.readBackLightValue();
            n = foundIndex(n, ref nBLLightLevel);
            this.label_BLLight.Text = "背光強度：" + (n+1).ToString();


            n = DeviceManager.readVolume();
            n = foundIndex(n, ref nVolume);
            this.label_Volumn.Text = "聲音大小：" + (n+1).ToString();

            LogManager.getInstance().Log("Initial SysConfig..OK");


            this.button_LogToggle.Visible = StoreData.getInstance().IsDebug;
            
        }



        private void button_Login_1_Click(object sender, EventArgs e)
        {
        }

        private void button_Login_Clear_Click(object sender, EventArgs e)
        {

        }
        string strFull = "０１２３４５６７８９＿";
        string strHalf =  "0123456789_" ;
        private void button_Login_Login_Click(object sender, EventArgs e)
        {

        }

        private void button_Reset_Click(object sender, EventArgs e)
        {
            CmdTalker.getInstance().SendServerAndWaitResponse("6000",5,"MOK",true);

            foreach (Control c in this.Controls)
            {
                c.Enabled = false;
            }

            MessageBox.Show("您可以放心關機了, 謝謝您.");

            
        }

        private void button_BB_Click(object sender, EventArgs e)
        {
            Form_Main.Main.ChangeUIFace(Color.White, Color.Black);
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            Form_Main.Main.ChangeToMainScreen();

        }

        private void button_WB_Click(object sender, EventArgs e)
        {
            Form_Main.Main.ChangeUIFace(Color.Black, Color.White);
        }


        private void button_LogToggle_Click(object sender, EventArgs e)
        {
            if (Form_Main.Main.button_Log.Visible)
                Form_Main.Main.button_Log.Visible = false;
            else
                Form_Main.Main.button_Log.Visible = true;

            Form_Main.Main.button_Log_Click(null, null);
            
        }


        private uint[] nBLLightLevel = { 0, 5, 15, 25, 35, 45, 55, 64 };
        private uint[] nBLTimeout = { 10, 20 ,30,  40, 60, 90, 120, 180, 300, int.MaxValue };
        private uint[] nVolume = { 0,  858993459,  1717986918, 2576980377, 3006444338, 3435973836, 3865437797 };
        //private uint[] nVolume = { 0, 429463961, 858993459, 1288457420, 1717986918, 2147450879, 2576980377, 3006444338, 3435973836, 3865437797, 4294967295 };
        private uint foundIndex(uint arg_nTarget, ref uint[] arg_Data)
        {
            uint n = 0;
            for (uint i = 0; i < arg_Data.Length; i++)
            {
                if (arg_Data[i] == arg_nTarget)
                {
                    n = i;
                    break;
                }
                else if(arg_Data[i] > arg_nTarget)
                {
                    n = i;
                    break;

                }
            }
            return n;

        }
        private void button_BLTAddX_Click(object sender, EventArgs e)
        {
            if (sender is Button)
            {
                Button btn = (Button)sender;
                LogManager.getInstance().Log(System.Convert.ToString(btn.Tag));
                string [] straryTag = System.Convert.ToString(btn.Tag).Split(';');
                uint [] data = null;
                switch(straryTag[1])
                {
                    case "ACBrightness":
                        data = nBLLightLevel;
                        break;
                    case "ACTimeout":
                        data = nBLTimeout;
                        break;
                    case "Volume":
                        data = nVolume;
                        break;
                }
                if (straryTag.Length == 6)
                {
                    uint n =  DeviceManager.readSettingValue(straryTag[0], straryTag[1], straryTag[2]);
                    try
                    {
                        LogManager.getInstance().Log("n1 is " + n.ToString());

                        n = foundIndex(n,ref data);
                        LogManager.getInstance().Log("n2 is " + n.ToString());
                        if (straryTag[3] == "+" && n < data.Length -1)
                        {
                            n += 1;
                        }
                        else if(straryTag[3] == "-" && n > 0)
                        {
                            n -= 1;
                        }
                        LogManager.getInstance().Log("n3 is " + n.ToString() + " val:" + data[n].ToString());

                        DeviceManager.setSettingValue(straryTag[0], straryTag[1], data[n], straryTag[5]);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error Para");
                        MessageBox.Show(ex.Message);
                    }
                    if (sender == this.button_BLTAdd || sender == this.button_BLTSub)
                    {
                        if (n== data.Length-1)
                            this.label_BLTimeout.Text = "背光時間：恒亮";
                        else
                            this.label_BLTimeout.Text = "背光時間：" + n.ToString();
                    }
                    if (sender == this.button_BLLAdd || sender == this.button_BLLSub)
                    {
                        this.label_BLLight.Text = "背光強度：" + n.ToString();
                    }
                    if (sender == this.button_VolAdd || sender == this.button_VolSub)
                    {
                        this.label_Volumn.Text = "聲音大小：" + n.ToString();
                    }
                }
                else
                {
                    MessageBox.Show("Error Para");
                }

            }
        }

        private void BLChange(bool arg_bAdd)
        {
            Form_Main.Main.resetBackLightTimeout();
            uint n = DeviceManager.readBackLightValue();
            try
            {
                LogManager.getInstance().Log("n1 is " + n.ToString());
                n = foundIndex(n, ref nBLLightLevel);
                LogManager.getInstance().Log("n2 is " + n.ToString());
                if (arg_bAdd)
                {
                    if (n < nBLLightLevel.Length - 1)
                    {
                        n += 1;
                    }
                }
                else
                {
                    if (n > 0)
                    {
                        n -= 1;
                    }
                }
                LogManager.getInstance().Log("n3 is " + n.ToString() + " val:" + nBLLightLevel[n].ToString());
                StoreData.getInstance().BackLightLevel = nBLLightLevel[n];
                DeviceManager.setBackLightValue(nBLLightLevel[n]);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Para");
                MessageBox.Show(ex.Message);
            }
            this.label_BLLight.Text = "背光強度：" + (n+1).ToString();
        }
        private void button_BLLAdd_Click(object sender, EventArgs e)
        {
            BLChange(true);

        }

        private void button_BLLSub_Click(object sender, EventArgs e)
        {
            BLChange(false);

        }

        private void BLTimeoutChange(bool arg_bAdd)
        {
            Form_Main.Main.resetBackLightTimeout();
            uint n = DeviceManager.readBackLightTimeOut();
            try
            {
                LogManager.getInstance().Log("n1 is " + n.ToString());
                n = foundIndex(n, ref nBLTimeout);
                LogManager.getInstance().Log("n2 is " + n.ToString());
                if (arg_bAdd)
                {
                    if (n < nBLTimeout.Length - 1)
                    {
                        n += 1;
                    }
                }
                else
                {
                    if (n > 0)
                    {
                        n -= 1;
                    }
                }
                LogManager.getInstance().Log("n3 is " + n.ToString() + " val:" + nBLTimeout[n].ToString());
                StoreData.getInstance().BackLightSecs = nBLTimeout[n];
                DeviceManager.setBackLightTimeOut(nBLTimeout[n]);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Para");
                MessageBox.Show(ex.Message);
            }
            if (n == nBLTimeout.Length - 1)
                this.label_BLTimeout.Text = "背光時間：恒亮";
            else
                this.label_BLTimeout.Text = "背光時間：" + nBLTimeout[n].ToString() + "秒";
        }
        private void button_BLTAdd_Click(object sender, EventArgs e)
        {
            BLTimeoutChange(true);
        }

        private void button_BLTSub_Click(object sender, EventArgs e)
        {
            BLTimeoutChange(false);
        }


        private void VolChange(bool arg_bAdd)
        {
            Form_Main.Main.resetBackLightTimeout();
            uint n = DeviceManager.readVolume();
            try
            {
                LogManager.getInstance().Log("n1 is " + n.ToString());
                n = foundIndex(n, ref nVolume);
                LogManager.getInstance().Log("n2 is " + n.ToString());
                if (arg_bAdd)
                {
                    if (n < nVolume.Length - 1)
                    {
                        n += 1;
                    }
                }
                else
                {
                    if (n > 0)
                    {
                        n -= 1;
                    }
                }
                LogManager.getInstance().Log("n3 is " + n.ToString() + " val:" + nVolume[n].ToString());
                StoreData.getInstance().SoundVol = nVolume[n];
                DeviceManager.setVolume(nVolume[n]);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Para");
                MessageBox.Show(ex.Message);
            }

            this.label_Volumn.Text = "聲音大小：" + (n + 1).ToString();

            SoundManager.getInstance().playMsgRecv();

        }

        private void button_VolAdd_Click(object sender, EventArgs e)
        {
            VolChange(true);
        }

        private void button_VolSub_Click(object sender, EventArgs e)
        {
            VolChange(false);
        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            //Form_Main.Main.resetBackLightTimeout();
            Form_Main.Main.ChangeToMainScreen();

        }

    }
}
